﻿using System;
using System.Drawing;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Xpedite.Localization;
using Xpedite.Resources;

/* Copyright (c) 2010 CBaxter
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), 
 * to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
 * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 
 * IN THE SOFTWARE. 
 */

namespace Xpedite.Test.Resources
{
  [TestClass]
  public class ResourceTest
  {
    [TestMethod]
    public void TestResourceDataPropertiesReturnSetValues()
    {
      var resource = new ResourceData
      {
        Bytes = Encoding.UTF8.GetBytes("Bytes"),
        Encoding = Encoding.UTF8,
        LastModified = DateTime.Now.Date
      };

      Assert.AreEqual("Bytes", Encoding.UTF8.GetString(resource.Bytes));
      Assert.AreEqual(Encoding.UTF8.WebName, resource.Encoding.WebName);
      Assert.AreEqual(DateTime.Now.Date, resource.LastModified);
    }

    [TestMethod]
    public void TestResourcePropertiesReturnSetValues()
    {
      var resource = new Resource
      {
        Bytes = Encoding.UTF8.GetBytes("Bytes"),
        Compressable = true,
        Type = ResourceType.Css,
        Encoding = Encoding.UTF8,
        LastModified = DateTime.Now.Date
      };

      Assert.AreEqual("Bytes", Encoding.UTF8.GetString(resource.Bytes));
      Assert.AreEqual(true, resource.Compressable);
      Assert.AreEqual(ResourceType.Css, resource.Type);
      Assert.AreEqual(Encoding.UTF8.WebName, resource.Encoding.WebName);
      Assert.AreEqual(DateTime.Now.Date, resource.LastModified);
    }

    [TestMethod]
    public void TestImageResourcePropertiesReturnSetValues()
    {
      var resource = new ImageResource
      {
        Bytes = Encoding.UTF8.GetBytes("Bytes"),
        Compressable = true,
        Type = ResourceType.Css,
        Encoding = Encoding.UTF8,
        LastModified = DateTime.Now.Date,
        Size = new Size(10,10)
      };

      Assert.AreEqual("Bytes", Encoding.UTF8.GetString(resource.Bytes));
      Assert.AreEqual(true, resource.Compressable);
      Assert.AreEqual(ResourceType.Css, resource.Type);
      Assert.AreEqual(Encoding.UTF8.WebName, resource.Encoding.WebName);
      Assert.AreEqual(DateTime.Now.Date, resource.LastModified);
      Assert.AreEqual(new Size(10, 10), resource.Size);
    }

    [TestMethod]
    public void TestCssResourceGetContentType()
    {
      var resource = new Resource
      {
        Bytes = Encoding.UTF8.GetBytes("Bytes"),
        Compressable = true,
        Type = ResourceType.Css,
        Encoding = Encoding.UTF8,
        LastModified = DateTime.Now.Date
      };

      Assert.AreEqual("text/css; charset=utf-8", resource.GetContentType());
    }

    [TestMethod]
    public void TestJavaScriptResourceGetContentType()
    {
      var resource = new Resource
      {
        Bytes = Encoding.UTF8.GetBytes("Bytes"),
        Compressable = true,
        Type = ResourceType.JavaScript,
        Encoding = Encoding.UTF8,
        LastModified = DateTime.Now.Date
      };

      Assert.AreEqual("text/javascript; charset=utf-8", resource.GetContentType());
    }

    [TestMethod]
    public void TestGifResourceGetContentType()
    {
      var resource = new Resource
      {
        Bytes = Encoding.UTF8.GetBytes("Bytes"),
        Compressable = true,
        Type = ResourceType.Gif,
        Encoding = Encoding.UTF8,
        LastModified = DateTime.Now.Date
      };

      Assert.AreEqual("image/gif", resource.GetContentType());
    }

    [TestMethod]
    public void TestJpegResourceGetContentType()
    {
      var resource = new Resource
      {
        Bytes = Encoding.UTF8.GetBytes("Bytes"),
        Compressable = true,
        Type = ResourceType.Jpeg,
        Encoding = Encoding.UTF8,
        LastModified = DateTime.Now.Date
      };

      Assert.AreEqual("image/jpeg", resource.GetContentType());
    }

    [TestMethod]
    public void TestPngResourceGetContentType()
    {
      var resource = new Resource
      {
        Bytes = Encoding.UTF8.GetBytes("Bytes"),
        Compressable = true,
        Type = ResourceType.Png,
        Encoding = Encoding.UTF8,
        LastModified = DateTime.Now.Date
      };

      Assert.AreEqual("image/png", resource.GetContentType());
    }

    [TestMethod]
    public void TestUnknownResourceGetContentType()
    {
      var resource = new Resource
      {
        Bytes = Encoding.UTF8.GetBytes("Bytes"),
        Compressable = true,
        Type = ResourceType.Unknown,
        Encoding = Encoding.UTF8,
        LastModified = DateTime.Now.Date
      };

      Expect.Exception(() => resource.GetContentType(), new XpediteException(String.Format(ExceptionMessages.InvalidResourceTypeSpecified, ResourceType.Unknown)));
    }
  }
}
